package com.villaday.idf.adapter3;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.model3.HotelRoomEntity;

import java.util.ArrayList;
import java.util.List;


public class FindHouseWineShopDetailsAdapter extends AbsRecyclerViewAdapter<HotelRoomEntity.BodyBean> {

    View.OnClickListener onClickListener;
    OnItemPositionClickListener onPhotoClickListener;

    public FindHouseWineShopDetailsAdapter(Context context, View.OnClickListener onClickListener) {
        super(context, R.layout.recycler_fhwsd);
        this.onClickListener = onClickListener;

    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, HotelRoomEntity.BodyBean d, final int position) {
        try {
            holder.bindTextView(R.id.vw_fhwsd_title, d.getName())
                    .bindTextView(R.id.tv_fhwsd_content, d.getDescription())
                    .bindTextViewWithHtml(R.id.tv_fhwsd_money_left, "&yen;")
//                    .bindTextViewWithHtml(R.id.tv_fhwsd_money,  (d.isMoreDay() ? d.getAveragePriceValue() : d.getCurrentPriceValue()))
                    .bindTextViewWithHtml(R.id.tv_fhwsd_money, d.getAveragePriceValue())
                    .bindTextView(R.id.tv_fhwsd_hign, d.getFloor() != null && d.getFloor().length() != 0 && !d.getFloor().equals("null") ? d.getFloor() + "层" : "");

            LinearLayout llMoney = (LinearLayout) holder.getView(R.id.ll_fhwsd_money);
            llMoney.setTag(position);
            llMoney.setOnClickListener(onClickListener);


            LinearLayout llMore = (LinearLayout) holder.getView(R.id.ll_fhwsd_more);
            llMore.setTag(position);
            llMore.setOnClickListener(onClickListener);

            ImageView ivMore = (ImageView) holder.getView(R.id.iv_fhwsd_money_right);
            TextView tvConfirm = (TextView) holder.getView(R.id.tv_fhwsd_confirm);
            tvConfirm.setTag(position);
            tvConfirm.setOnClickListener(onClickListener);

            if (d.isVip()) {
                if (d.getAddBrokerageAmountValue() != null && !d.getAddBrokerageAmountValue().equals("0")) {
                    holder.getView(R.id.vw_fhwsd_money_bt2).setVisibility(View.VISIBLE);
                    holder.bindTextViewWithHtml(R.id.vw_fhwsd_money_bt2, "加返 &yen;" + d.getAddBrokerageAmountValue() + "起");
                }

                if (d.getAddBrokerageAmountValue() != null) {
                    holder.getView(R.id.vw_fhwsd_money_bt1).setVisibility(View.VISIBLE);
                    holder.bindTextViewWithHtml(R.id.vw_fhwsd_money_bt1, "立返 &yen;" + d.getBrokerageAmountValue() + "起");
                }
            } else {
                holder.getView(R.id.vw_fhwsd_money_bt2).setVisibility(View.GONE);
                holder.getView(R.id.vw_fhwsd_money_bt1).setVisibility(View.GONE);
            }
            TextView tvLeft0 = (TextView) holder.getView(R.id.tv_fhwsd_money_left0);
            TextView tvLeft = (TextView) holder.getView(R.id.tv_fhwsd_money_left);
            TextView tvMoney = (TextView) holder.getView(R.id.tv_fhwsd_money);
            TextView tvMoneyRight = (TextView) holder.getView(R.id.tv_fhwsd_money_right);
            TextView textView = (TextView) holder.getView(R.id.tv_fhwsd_confirm);
            View vwLeft = holder.getView(R.id.vw_fhwsd_left);
            TextView tvTitle = (TextView) holder.getView(R.id.vw_fhwsd_title);
            LinearLayout llItemMore = (LinearLayout) holder.getView(R.id.ll_fhwsd_more);
            llItemMore.setTag(position);
            llItemMore.setOnClickListener(onClickListener);

            if (d.isNotBooked()) {
                tvTitle.setTextColor(context.getResources().getColor(R.color.colorTextLight));
                vwLeft.setBackgroundColor(context.getResources().getColor(R.color.colorTextLight));
                tvMoney.setTextColor(context.getResources().getColor(R.color.colorTextLight));
                tvLeft.setTextColor(context.getResources().getColor(R.color.colorTextLight));
                textView.setBackground(context.getResources().getDrawable(R.drawable.shape_solid_gray90));
                tvLeft0.setTextColor(context.getResources().getColor(R.color.colorTextLight));
                textView.setEnabled(false);
            } else {
                tvTitle.setTextColor(context.getResources().getColor(R.color.colorTextBlack));
                vwLeft.setBackgroundColor(context.getResources().getColor(R.color.colorF73637));
                tvMoney.setTextColor(context.getResources().getColor(R.color.colorF73637));
                tvLeft.setTextColor(context.getResources().getColor(R.color.colorF73637));
                textView.setBackground(context.getResources().getDrawable(R.drawable.shape_fe_ff40));
                tvLeft0.setTextColor(context.getResources().getColor(R.color.colorF73637));
                textView.setEnabled(true);
            }

            View viewLine = holder.getView(R.id.vw_fhwsd_line);


            /**
             * 子列表适配
             */
            if (d.getSalePlans().size() >= 2) {
                ivMore.setVisibility(View.VISIBLE);
                tvMoneyRight.setVisibility(View.VISIBLE);
                tvConfirm.setVisibility(View.GONE);
                tvLeft0.setVisibility(View.GONE);

                RecyclerView recyclerView = (RecyclerView) holder.getView(R.id.rv_fhwsd_near);
                if (d.isOpen()) {
                    recyclerView.setVisibility(View.VISIBLE);
                    viewLine.setVisibility(View.GONE);
                    ivMore.setImageResource(R.drawable.text_down);
                } else {
                    recyclerView.setVisibility(View.GONE);
                    viewLine.setVisibility(View.VISIBLE);
                    ivMore.setImageResource(R.drawable.text_up);
                }


                recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                FindHouseWindShopMoreAdapter findHouseWindShopMoreAdapter = new FindHouseWindShopMoreAdapter(context);
                recyclerView.setAdapter(findHouseWindShopMoreAdapter);
                List<HotelRoomEntity.BodyBean.SalePlansBean> saleData = new ArrayList<>();
                if (d.getSalePlans().size() > 5 && !d.isItemOpen()) {
                    if (d.isOpen()) {
                        llItemMore.setVisibility(View.VISIBLE);
                    } else {
                        llItemMore.setVisibility(View.GONE);
                    }

                    for (int i = 0; i < 5; i++) {
                        saleData.add(d.getSalePlans().get(i));
                    }
                } else {
                    saleData = d.getSalePlans();
                    llItemMore.setVisibility(View.GONE);
                }
                findHouseWindShopMoreAdapter.setData(saleData);
                findHouseWindShopMoreAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View v, int itemPosition) {
                        onPhotoClickListener.onItemPositionClick(v, position, itemPosition);
                    }
                });
                findHouseWindShopMoreAdapter.setOnEnergyItemClickListener(new FindHouseWindShopMoreAdapter.OnItemPositionClickListener() {
                    @Override
                    public void onItemPositionClick(View v, int itemPosition) {
                        onPhotoClickListener.onItemPositionClick(v, position, itemPosition);
                    }
                });
            } else {
                if (d.isMoreDay()) {
                    tvLeft0.setVisibility(View.VISIBLE);
                } else {
                    tvLeft0.setVisibility(View.GONE);
                }

                tvMoneyRight.setVisibility(View.GONE);
                ivMore.setVisibility(View.GONE);
                tvConfirm.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * Item点击监听 -- 添加头部后，头部的点击事件需自行处理
     */
    public void setOnEnergyItemClickListener(OnItemPositionClickListener onPhotoClickListener) {
        this.onPhotoClickListener = onPhotoClickListener;
    }


    public interface OnItemPositionClickListener {
        void onItemPositionClick(View view, int position, int itemPosition);
    }
}
